﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DeliveryDateManager : ASPXBaseClass
{
    GZ.Entity.EDeliveryPeriod dllEDeliveryPeriod = new GZ.Entity.EDeliveryPeriod();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"].ToString() != "admin")
        {
            Response.Redirect("~/Admin/RedirectLogin.htm");
        }
        if (!IsPostBack)
        {
            BindRpt();
        }
    }

    /// <summary>
    /// 绑定Reapeter列表
    /// </summary>
    void BindRpt()
    {
        this.rptDeliveryPeriod.DataSource = dllEDeliveryPeriod.GetDeliveryPeriod();
        this.rptDeliveryPeriod.DataBind();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        GZ.Entity.EDeliveryPeriod obj = new GZ.Entity.EDeliveryPeriod();

        if (this.HId.Value.Length == 0)
        {
            obj.DeliveryPeriodName = this.txtDeliverPeroid.Text;
            obj.EndTime = Convert.ToDateTime("1980-1-1 " + this.txtDeliveryEnd.Text);
            if (dllEDeliveryPeriod.Insert(obj))
            {
                BindRpt();
                this.txtDeliveryEnd.Text = string.Empty;
                this.txtDeliverPeroid.Text = string.Empty;
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('添加失败');</script>");
            }
        }
        else
        {
            obj.Id = Convert.ToInt32(this.HId.Value);
            obj.DeliveryPeriodName = this.txtDeliverPeroid.Text;
            obj.EndTime = Convert.ToDateTime("1980-1-1 " + this.txtDeliveryEnd.Text);
            if (dllEDeliveryPeriod.Modify(obj))
            {
                BindRpt();
                this.txtDeliveryEnd.Text = string.Empty;
                this.txtDeliverPeroid.Text = string.Empty;
                this.btnAdd.Text = "添加";
                this.HId.Value = "";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('修改失败');</script>");
            }
        }
    }


    /// <summary>
    /// Reapeater命令事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptDeliveryPeriod_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int Id = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Del")
        {
            if (dllEDeliveryPeriod.Del(Id))
            {
                BindRpt();
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('删除失败');</script>");
            }
        }
        if (e.CommandName == "Modify")
        {
            this.btnAdd.Text = "保存";
            GZ.Entity.EDeliveryPeriod obj = new GZ.Entity.EDeliveryPeriod();

            IList<GZ.Entity.EDeliveryPeriod> list = dllEDeliveryPeriod.GetDeliveryPeriodById(Id);
            this.txtDeliverPeroid.Text = list[0].DeliveryPeriodName;
            this.txtDeliveryEnd.Text = list[0].EndTime.ToString().Replace("1980-1-1 ", "").Replace("1980/1/1 ","").Trim();
            this.HId.Value = list[0].Id.ToString();
        }
    }
}